﻿using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;

namespace BadOCR.Common
{
	public class SougouOCR
	{

		public SougouOCR() { }

		public string General(Image image)
		{
			string text = "------WebKitFormBoundary0123456789abcdef";
			var zoomImage = this.ZoomImage((Bitmap)image, 120, 120);
			byte[] b = this.Image2ByteArray(zoomImage);
			string s = text + "\r\nContent-Disposition: form-data; name=\"pic\"; filename=\"pic.jpg\"\r\nContent-Type: image/jpeg\r\n\r\n";
			string s2 = "\r\n" + text + "--\r\n";
			byte[] bytes = Encoding.ASCII.GetBytes(s);
			byte[] bytes2 = Encoding.ASCII.GetBytes(s2);
			byte[] array = Mergebyte(bytes, b, bytes2);
			HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create("http://ocr.shouji.sogou.com/v2/ocr/json");
			httpWebRequest.Timeout = 8000;
			httpWebRequest.Method = "POST";
			httpWebRequest.ContentType = "multipart/form-data; boundary=" + text.Substring(2);
			using (Stream requestStream = httpWebRequest.GetRequestStream())
			{
				requestStream.Write(array, 0, array.Length);
			}
			Stream responseStream = ((HttpWebResponse)httpWebRequest.GetResponse()).GetResponseStream();
			var responseText = new StreamReader(responseStream, Encoding.GetEncoding("utf-8")).ReadToEnd();
			responseStream.Close();

			var ocrResult = JObject.Parse(responseText);
			var sb = new StringBuilder();
			if (ocrResult.Value<int>("success") == 1)
			{
				var words_result = ocrResult["result"] as JArray;
				if (words_result != null)
				{
					foreach (var word in words_result)
						sb.Append(word["content"].ToString());
				}
			}

			return sb.ToString();
		}

		private Bitmap ZoomImage(Bitmap bitmap1, int destHeight, int destWidth)
		{
			double num = (double)bitmap1.Width;
			double num2 = (double)bitmap1.Height;
			if (num < (double)destHeight)
			{
				while (num < (double)destHeight)
				{
					num2 *= 1.1;
					num *= 1.1;
				}
			}
			if (num2 < (double)destWidth)
			{
				while (num2 < (double)destWidth)
				{
					num2 *= 1.1;
					num *= 1.1;
				}
			}
			int width = (int)num;
			int height = (int)num2;
			Bitmap bitmap2 = new Bitmap(width, height);
			Graphics graphics = Graphics.FromImage(bitmap2);
			graphics.DrawImage(bitmap1, 0, 0, width, height);
			graphics.Save();
			graphics.Dispose();
			return new Bitmap(bitmap2);
		}

		private byte[] Image2ByteArray(Image img)
		{
			byte[] result;
			try
			{
				MemoryStream memoryStream = new MemoryStream();
				img.Save(memoryStream, ImageFormat.Jpeg);
				byte[] array = new byte[memoryStream.Length];
				memoryStream.Position = 0L;
				memoryStream.Read(array, 0, (int)memoryStream.Length);
				memoryStream.Close();
				result = array;
			}
			catch
			{
				result = null;
			}
			return result;
		}

		private byte[] Mergebyte(byte[] a, byte[] b, byte[] c)
		{
			byte[] array = new byte[a.Length + b.Length + c.Length];
			a.CopyTo(array, 0);
			b.CopyTo(array, a.Length);
			c.CopyTo(array, a.Length + b.Length);
			return array;
		}
	}
}
